<template>
  <div class="user-management-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-select
          v-model="queryForm.locked"
          clearable
          placeholder="请选择锁定状态"
        >
          <el-option
            v-for="item in options1"
            :key="item.id"
            :label="item.creClasName"
            :value="item.id"
            @click.native="optosall(item)"
          />
        </el-select>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.keyWord"
              clearable
              placeholder="请输入用户名"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>
    <el-table
      v-loading="listLoading"
      border
      :data="list"
      @selection-change="setSelectRows"
    >
      <el-table-column align="center" show-overflow-tooltip type="selection" />
      <el-table-column
        align="center"
        label="ID"
        prop="id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="文章标题"
        prop="articleTitle"
        show-overflow-tooltip
      />
      <!-- <el-table-column
        align="center"
        label="文章内容"
        prop="articleContent"
        show-overflow-tooltip
      /> -->

      <el-table-column align="center" label="锁定状态" show-overflow-tooltip>
        <template slot-scope="scope">
          <template v-if="scope.row.locked == 1">
            <span style="color: red">锁定</span>
          </template>
          <template v-else-if="scope.row.locked == 0">
            <span style="color: green">启用</span>
          </template>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="创建时间"
        prop="createTime"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="操作"
        show-overflow-tooltip
        width="130"
      >
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-image
          class="vab-data-empty"
          :src="require('@/assets/empty_images/data_empty.png')"
        />
      </template>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="edit" @fetch-data="fetchData" />
    <editting ref="editting" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { getwoList, dodelete } from '@/api/xieyi' //doDelete,
  import editting from './model/tingyong.vue'

  import Edit from './model/edit.vue'

  export default {
    name: 'Notice',
    components: { Edit, editting },
    data() {
      return {
        options1: [
          {
            id: '0',
            creClasName: '未锁定',
          },
          {
            id: '1',
            creClasName: '锁定',
          },
        ], //锁定状态
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 10,
          locked: '', //锁定状态 0未锁定 1锁定
          keyWord: '', //关键词
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      optosall() {
        //选择状态查询

        this.queryForm.pageNum = 1
        this.fetchData()
      },
      setSelectRows(val) {
        this.selectRows = val
      },
      handleEdit(row) {
        this.$refs['edit'].showEdit(row)
      },
      handleting(row) {
        if (row.id) {
          this.$refs['editting'].showEdit(row)
        } else {
          this.$refs['editting'].showEdit()
        }
      },
      handleDelete(row) {
        if (row.id) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await dodelete({ id: row.id })
            this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            await this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            const id = this.selectRows.map((item) => item.id).join()
            this.$baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await dodelete({ id })
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
              await this.fetchData()
            })
          } else {
            this.$baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },

      async fetchData() {
        this.listLoading = true
        const res = await getwoList(this.queryForm)

        this.list = res.data.records

        this.total = res.data.total
        this.listLoading = false
      },
    },
  }
</script>
